<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>点检项管理</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<style type="text/css">
		 .left-label {
		 	width: 120px !important
		 }
	</style>
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
</head>
<body>
<div class="demoTable" style="padding:10px 0 0 15px">
    <div class="layui-inline">
    	<%--<input class="layui-input" name="searchTime" id="searchTime" placeholder="请输入时间检索" style="display: inline; width: 45%">--%>
        <input class="layui-input" name="keyword" id="keyword" placeholder="请输入巡检项名称" autocomplete="off" style="display: inline; width: 100%">
    </div>
    <button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">搜索</i></button>
</div>
<table class="layui-hide" id="tableList" lay-filter="tableList"></table>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="add"><i class="layui-icon layui-icon-add-1">新增</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="delete"><i class="layui-icon layui-icon-delete">批量删除</i></button>
	</div>
</script>
 
<script type="text/html" id="rowToolbar">
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-xs" lay-event="add1">巡检项详细新增</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="rowToolbar2">
	<a class="layui-btn layui-btn-xs" lay-event="edit2">编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del2">删除</a>
</script>
              
<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/layui.all.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>
 
<script type="text/javascript">
	layui.use(['table','layer','upload','form','laydate'], function(){
		var table = layui.table;
		var layer = layui.layer;
	    var form = layui.form;
		
		var $ = layui.jquery, active = {
			reload:function () {
				/*var searchTime = $("#searchTime").val();*/
				var keyword01 = $("#keyword").val();
				/*console.log(searchTime + ',' + keyword)*/
				table.reload('contenttable',{
					method:'get',
					where:{"keyword01":keyword01},
					page: {
						curr: 1
					}
				});
			}
		}
		$('.layui-btn').on('click', function(){
	        var type = $(this).data('type');
	        active[type] ? active[type].call(this) : '';
	    });
		table.render({
		    elem: '#tableList'
		    ,url:'${pageContext.request.contextPath }/deviceAlarm/queryCheckItem.do'
		    ,toolbar: '#toolbar'
		    ,title: '巡检项'
		    ,id :'contenttable'
		    ,limits:[10,20,30]
		    ,cols: [[
		      {type: 'checkbox', fixed: 'left', },
		      {field:'', title:'序号', sort: true, type:'numbers', width: 60},
		      {field:'checkItemName', title:'维护维修巡检项名称'},
			  {field:'op1', title:'维护维修巡检项详细', templet:function (rowData){
              	return "<a href='javascript:void(0)' style='color:#4D61B3' onclick='checkItemNameDetail(\""+rowData.id+"\")'>"+'维护维修巡检项详细'+"</a>";
              }},
		      {fixed: 'right', title:'操作', toolbar: '#rowToolbar', width: 220}
		    ]],
		    page: true
		});
	  
		//头工具栏事件
		table.on('toolbar(tableList)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id);
		    switch(obj.event){
                //新增
		    	case 'add':
			    	layer.open({
						type: 1 					//Page层类型
				  		,area: ['500px', '200px'] 	//宽  高
				  		,title: '新增'
				  		,shade: 0.6 				//遮罩透明度
				  		,maxmin: true 				//允许全屏最小化
				  		,anim: 1 					//0-6的动画形式，-1不开启
				  		,content: '<div id="addDivID">'+
				  					'<form class="layui-form" id="addFormID">'+
										'<label class="layui-form-label left-label" style="margin-top: 15px">巡检项名称</label><input class="layui-input" id="checkItemName" name="checkItemName" lay-verify="checkItemName" style="width:300px; margin-top:15px; display: inline;">' +
											'<div class="layui-inline" style="margin-top: 4px;">'+
												'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
											'</div>'+
										'<br>'+
										'<button class="layui-btn layui-btn-blue" id="subBtn" lay-submit lay-filter="addForm" style="margin-left:160px; margin-top: 20px">提交</button>&emsp;&emsp;&emsp;&emsp;'+
										'<button type="reset" class="layui-btn layui-btn-primary" style="margin-top: 20px">重置</button>'+
								  	'</form>'+
								  '</div>'
					});
		      	break;
		      	//批量删除
		    	case 'delete':
		        	var data = checkStatus.data;
		        	var idArr = new Array();
		        	if(data.length == 0){
		        		toastr.warning("请至少选择一条记录！");
		        	}
		        	for(var i=0;i < data.length;i++){
		        		/*alert(data[i].id)*/
						idArr[i] = data[i].id;
		        	}
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath }/deviceAlarm/deleteCheckItemById.do',
                        data:{"idArr" : idArr},
                        success:function(data){
                            layer.confirm('确定删除吗？', function(index){
                                if(data > 0){
                                    toastr.success("删除成功！");
                                    setTimeout(function(){
                                        location.reload();
                                    },1000);
                                }else{
                                	toastrStyle();
                                    toastr.warning("删除失败！");
                                    setTimeout(function(){
        								location.reload();
                                    },1000);
                                }
							});
                        }
                    });
		      	break;
		      	case 'select':
		      		/*console.log(obj)*/
		      	break;
		    };
		});
	  
		//监听行工具事件
		table.on('tool(tableList)', function(obj){
			var data = obj.data;
			//alert(data.id)
            var idArr = new Array();
			//单个删除
			if(obj.event === 'del'){
			  layer.confirm('确定删除吗？', function(index){
			    var id = obj.data.id;
			    idArr[0] = id;
			    $.ajax({
			    	type:'post',
			    	url:'${pageContext.request.contextPath }/deviceAlarm/deleteCheckItemById.do',
                    data:{ "idArr" : idArr },
			    	success:function(data){
			    		if(data > 0){
                            toastrStyle();
                            toastr.success("删除成功！");
                            setTimeout(function(){
								location.reload();
                            },1000);
			    		}else{
                            toastrStyle();
                            toastr.warning("删除失败！");
                            setTimeout(function(){
								location.reload();
                            },1000);
			    		}
			    	}
			    });
			    layer.close(index);
			  });
			} else if(obj.event === 'edit'){
				layer.open({
				  type: 1 							//Page层类型
				  ,area: ['500px', '200px']			//宽  高
				  ,title: '编辑'
				  ,shade: 0.6 						//遮罩透明度
				  ,maxmin: true 					//允许全屏最小化
				  ,anim: 1 							//0-6的动画形式，-1不开启
				  ,content: '<form class="layui-form" id="editFormID">'+
					'<input class="layui-hide" id="id" name="id" value="'+data.id+'">'+
					'<label class="layui-form-label left-label">巡检项名称</label><input class="layui-input" id="checkItemName" name="checkItemName" 	value="'+data.checkItemName+'" lay-verify="checkItemName" style="width:300px; margin-top:15px"><br>'+
					'<button class="layui-btn layui-btn-blue" lay-submit lay-filter="editForm" style="margin-left:160px; margin-bottom: 20px">提交</button>&emsp;&emsp;&emsp;&emsp;<button type="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px">重置</button>'+
				'</form>'
				});
			} else if(obj.event === 'add1'){
                layer.open({
                    type: 1 							//Page层类型
                    ,area: ['500px', '200px']			//宽  高
                    ,title: '巡检项详细新增'
                    ,shade: 0.6 						//遮罩透明度
                    ,maxmin: true 						//允许全屏最小化
                    ,anim: 1 							//0-6的动画形式，-1不开启
                    ,content: '<form class="layui-form" id="addFormID1">'+
                        '<input class="layui-hide" id="cdId" name="cdId" value="'+data.id+'">'+
                        '<label class="layui-form-label left-label" style="margin-top: 15px">巡检项详细</label><input class="layui-input" id="checkItemDetailName" name="checkItemDetailName" lay-verify="checkItemDetailName" style="width:300px; margin-top:15px; display: inline;">'+
							'<div class="layui-inline" style="margin-top: 4px;">'+
								'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
							'</div>'+
						'<button class="layui-btn layui-btn-blue" id="subBtn1" lay-submit lay-filter="addForm1" style="margin-left:160px; margin-bottom: 20px; margin-top: 20px">提交</button>&emsp;&emsp;&emsp;&emsp;<button type="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px; margin-top: 20px">重置</button>'+
                        '</form>'
                });
			}
		});

        //监听第二个table的行工具事件
        table.on('tool(lookupTable1)', function(obj){
            var data = obj.data;
            //alert(data.id)
            //var idArr = new Array();
            //单个删除
            if(obj.event === 'del2'){
                layer.confirm('确定删除吗？', function(index){
                    var id = obj.data.id;
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath }/deviceAlarm/deleteCheckItemDetailById.do?id='+id,
                        /*data:{ "idArr" : idArr },*/
                        success:function(data){
                            if(data > 0){
                                toastrStyle();
                                toastr.success("删除成功！");
                                setTimeout(function(){
                                    location.reload();
                                },1000);
                            }else{
                                toastrStyle();
                                toastr.warning("删除失败！");
                                setTimeout(function(){
                                    location.reload();
                                },1000);
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if(obj.event === 'edit2'){
                layer.open({
                    type: 1 						//Page层类型
                    ,area: ['500px', '200px']		//宽  高
                    ,title: '编辑'
                    ,shade: 0.6 					//遮罩透明度
                    ,maxmin: true 					//允许全屏最小化
                    ,anim: 1 						//0-6的动画形式，-1不开启
                    ,content: '<form class="layui-form" id="editFormID2">'+
                        '<input class="layui-hide" id="id" name="id" value="'+data.id+'">'+
                        '<label class="layui-form-label left-label">巡检项详细</label><input class="layui-input" id="checkItemDetailName" name="checkItemDetailName" 	value="'+data.checkItemDetailName+'" lay-verify="checkItemDetailName" style="width:300px; margin-top:15px"><br>'+
                        '<button class="layui-btn layui-btn-blue" lay-submit lay-filter="editForm2" style="margin-left:160px; margin-bottom: 20px">提交</button>&emsp;&emsp;&emsp;&emsp;<button type="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px">重置</button>'+
                        '</form>'
                });
			}
        });

	    /**
         * 通用表单提交(AJAX方式)(新增)
         */
	    form.on('submit(addForm)', function (data) {
	    	$("#subBtn").attr("disabled",true);
	    	$.ajax({
				url : '${pageContext.request.contextPath}/deviceAlarm/addCheckItem.do',
                data: $("#addFormID").serialize(),
				cache : false,
				type : "post",
				}).done(
					function(res) {
						if (res > 0) {
                            toastrStyle();
                            toastr.success('新增成功！');
                            setTimeout(function(){
                                location.reload();
                            },1000);
						}
					}
				).fail(
					function(res) {
                        toastrStyle();
                        toastr.error('新增失败！');
                        setTimeout(function(){
							location.reload();
                        },1000);
					}
				)
				return false;
	    });
        /**
         * 通用表单提交(AJAX方式)(新增详细)
         */
        form.on('submit(addForm1)', function (data) {
        	$("#subBtn1").attr("disabled",true);
            $.ajax({
                url : '${pageContext.request.contextPath}/deviceAlarm/addCheckItemDetail.do',
                data: $("#addFormID1").serialize(),
                cache : false,
                type : "post",
            }).done(
                function(res) {
                    if (res > 0) {
                        toastrStyle();
                        toastr.success('新增成功！');
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }
                }
            ).fail(
                function(res) {
                    toastrStyle();
                    toastr.error('新增失败！');
                    setTimeout(function(){
                        location.reload();
                    },1000);
                }
            )
            return false;
        });
	    /**
	     * 通用表单提交(AJAX方式)(修改)
	     */
	    form.on('submit(editForm)', function (data) {
	    	$.ajax({
				url : '${pageContext.request.contextPath}/deviceAlarm/editCheckItemById.do',
                data: $("#editFormID").serialize(),
				cache : false,
				type : "post",
				}).done(
					function(res) {
						if (res > 0) {
                            toastrStyle();
                            toastr.success('修改成功！');
							setTimeout(function(){
								location.reload();
                            },1000);
						}
					}
				).fail(
					function(res) {
                        toastrStyle();
                        toastr.error('修改失败！');
                        setTimeout(function(){
							location.reload();
                        },1000);
					}
				)
				return false;
	    });

        /**
         * 通用表单提交(AJAX方式)(修改巡检项详细)
         */
        form.on('submit(editForm2)', function (data) {
            $.ajax({
                url : '${pageContext.request.contextPath}/deviceAlarm/editCheckItemDetailById.do',
                data: $("#editFormID2").serialize(),
                cache : false,
                type : "post",
            }).done(
                function(res) {
                    if (res > 0) {
                        toastrStyle();
                        toastr.success('修改成功！');
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }
                }
            ).fail(
                function(res) {
                    toastrStyle();
                    toastr.error('修改失败！');
                    setTimeout(function(){
                        location.reload();
                    },1000);
                }
            )
            return false;
        });

        /**
         * 新增表单校验
         */
        form.verify({
            //value：表单的值item：表单的DOM对象
            checkItemName: function(value, item){
                if(value == ''){
                    return '设备巡检项不能为空';
                }
            },
            checkItemDetailName: function(value, item){
                if(value == ''){
                    return '巡检项详细不能为空';
                }
			}
        });

	});

	toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上

    //提示框样式
    function toastrStyle(){
        toastr.options = {
            "closeButton": false,
            "debug": false,
            "newestOnTop": false,
            "progressBar": false,
            "positionClass": "toast-top-center",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "5000",
            "hideDuration": "1000",
            "timeOut&quot": "100000",
            "extendedTimeOut": "0",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        };
    }

    //维护维修项详细
    function checkItemNameDetail(cdId){
        //alert(cdId);
        layer.open({
            type: 1 					//Page层类型
            ,area: ['500px', '450px']   //宽  高
            ,title: '维护维修项详细'
            ,shade: 0.6 				//遮罩透明度
            ,maxmin: true 			    //允许全屏最小化
            ,anim: 1 					//0-6的动画形式，-1不开启
            ,content: '<table class="layui-hide" id="lookupTable1" lay-filter="lookupTable1"></table>'
            ,success: function(){
                layui.use(['table'], function(){
                    var table = layui.table;
                    table.render({
                        elem: '#lookupTable1'
                        ,url:'${pageContext.request.contextPath }/deviceAlarm/queryCheckItemDetailById.do?cdId='+cdId
                        ,title: '维护维修项详细'
                        ,limits:[10,20,30]
                        ,cols: [[
                            /*{type: 'checkbox', fixed: 'left'},*/
                            {field:'id', title:'序号', width: 100, fixed: 'left', unresize: true, sort: true, type:'numbers'},
                            {field:'checkItemDetailName', title:'维护维修巡检项详细'},
                            {unresize: true, title:'操作', toolbar: '#rowToolbar2', align: 'center'}
                        ]]
                        ,page: false
                    });
                });
            }
        });
	}
</script>

</body>
</html>